package org.hackystat.app.projectstatus.alert;

import java.util.Iterator;
import java.util.List;

import org.hackystat.app.projectstatus.view.AlertEmailManager;
import org.hackystat.app.stdcmd.alert.dailydiarycsv.DailyDiaryCsvAnalysisSelector;
import org.hackystat.core.kernel.admin.ServerProperties;
import org.hackystat.core.kernel.sdt.SdtManager;
import org.hackystat.core.kernel.sdt.SensorDataType;
import org.hackystat.core.kernel.sensordata.SensorDataIndex;
import org.hackystat.core.kernel.user.User;
import org.hackystat.core.kernel.util.Day;

/**
 * Implements an Alert singleton.
 * 
 * @author Josh
 *
 */
public class ProjectStatusDailyAlert {
	
	/** Initially the alert is turned off. */
	private boolean isEnabled = false;

  /** Creates the singleon Alert instance.  */
  public ProjectStatusDailyAlert() { }


  /**
   * Returns true if this alert is enabled by this user and if any sensor data
   * exists for this user for the day prior to the passed day.
   *
   * @param user  The user of interest.
   * @param day   The day of interest.
   * @return      True if this alert is enabled and sensor data exists, false
   *      otherwise.
   */
  public boolean isTriggered(User user, Day day) {
	  AlertEmailManager mailer = new AlertEmailManager();
	    // If the user has disabled this alert, then return false immediately.
	    if (!mailer.isEnabled(user)) {
	      return false;
	    }

	    // Otherwise, the alert is enabled, so see if it's triggered.
	    Day yesterday = day.inc(-1);
	    try {
	      SensorDataIndex userIndex = SensorDataIndex.getInstance(user.getUserKey());
	      for (Iterator i = SdtManager.getInstance().iterator(); i.hasNext();) {
	        SensorDataType sdt = (SensorDataType) i.next();
	        // Don't check Bad Data logs, we do that with BadDataAlert.
	        if (!("BadData".equals(sdt.getName()))) {
	          if (userIndex.dataExists(sdt, yesterday)) {
	            return true;
	          }
	        }
	      }
	    }
	    catch (Exception e) {
	      ServerProperties.getInstance().getLogger().
	        warning("Error during NewDataAlert trigger processing: " + e);
	    }
	    return false;
  }


  /**
   * Returns a string containing a one line explanation for why the event was
   * triggered.
   *
   * @param user  The user of interest.
   * @param day   The day of interest.
   * @return      The triggerExplanation value.
   */
  public String getTriggerExplanation(User user, Day day) {
    return "Sends an email indicating the daily project details of the project designated";
  }


  /**
   * Returns an URL that will invoke a command to display SensorData summary
   * statistics on the day prior to the passed day. This provides details on
   * exactly what data was received that led to the triggering of this alert.
   *
   * @param user  The user of interest.
   * @param day   The day of interest.
   * @return      The displayUrl value.
   */
  public String getDisplayUrl(User user, Day day) {
    Day yesterday = day.inc(-1);
    StringBuffer url = new StringBuffer();
    url.append(ServerProperties.getInstance().getHackystatHome()
        + "controller"
        + "?Key=" + user.getUserKey()
        + "&Command=" + "DailyProjectDetails"
        + "&StartDay=" + yesterday.getDayString()
        + "&StartMonth=" + yesterday.getMonthString()
        + "&StartYear=" + yesterday.getYearString());
    List names = DailyDiaryCsvAnalysisSelector.getSelectedAnalysisList(user);
    for (Iterator i = names.iterator(); i.hasNext();) {
       String name = (String)i.next();
       url.append("&" + name + "=true");
    };
    String urlString = url.toString();
    return urlString;
  }


  /**
   * Returns true if NewDataAlert is enabled by default.
   *
   * @param user  The user of interest.
   * @return      True.
   */
  public boolean isEnabledByDefault(User user) {
    return false;
  }

}
